from Example import Example 

def putIntoMap(map, label, example):
    if not map.has_key(label):
        map[label] = []
    map[label].append(example) 
    

def getDataAndDivide(fileName):
    '''
    returns tuple (trening_map, test_map), where map is mapping class to list of its examples
    '''
    
    f = open(fileName,"r")
    data_trening = {}
    data_test = {}
    i = 0
    for line in f.readlines():
        words = line.split(",")
        label = words[len(words)-1].strip()
        attrs = [float(x) for x in words[:-1]]
        print attrs
        example = Example(attrs, label)
        if i % 4 == 0:
            putIntoMap(data_test, label, example)
        else: 
            putIntoMap(data_trening, label, example)
        i = i + 1
    return (data_trening, data_test)

def getData(fileName):
    '''
    returns list of examples from file
    '''
    
    f = open(fileName,"r")
    examples = {}
    for line in f.readlines():
        words = line.split(",")
        label = words[len(words)-1].strip()
        attrs = [float(x) for x in words[:-1]]
        print attrs
        example = Example(attrs, label)
        putIntoMap(examples, label, example)
    return examples


if __name__ == "__main__":
    data = getDataAndDivide("iris.data")
    print data